package com.firemoss.magicbus.frameworks.swiz
{
	import com.firemoss.magicbus.messaging.AbstractConsumerMediator;
	
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	
	public class SwizConsumerMediator extends AbstractConsumerMediator
	{

		private var _dispatcher : IEventDispatcher
		[Dispatcher]
		public function set swizDispatcher( value :IEventDispatcher ) : void
		{
			_dispatcher = value
				
			if ( _conReqEvents )
			{
				subscribeToConnectionRequests( _conReqEvents )
			}
		}
		public function get swizDispatcher() : IEventDispatcher
		{
			return _dispatcher
		}
		
		private var _conReqEvents : Array
		
		override public function subscribeToConnectionRequests( eventNames : Array ) : void
		{
			var eventName : String 
			
			if ( _conReqEvents && swizDispatcher ) 
			{
				for each ( eventName in _conReqEvents )
				{ 
					// Handle both bubbled UI and centrally dispatched events
					swizDispatcher.removeEventListener( eventName, connectionRequested )
				}
			}
			
			_conReqEvents = eventNames
		
			if ( _conReqEvents && swizDispatcher )
			{
				for each ( eventName in _conReqEvents )
				{
					swizDispatcher.addEventListener( eventName, connectionRequested, false, 0, true )
				}
			}
		}
		
		override public function dispatch(event:Object):void
		{
			swizDispatcher.dispatchEvent( Event( event ) )
		}
		
		public function SwizConsumerMediator()
		{
			super();
		}
		
	}
}